<?php
namespace app\admin\controller;
use think\Db;
class Cates extends Base{
    //分类级别设置
    public function catelevel(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
        }
    }
    public function cate_list(){
        $cates=Db::name('cates')->where('state','<',3)->select();
        $faths=$cates;
        $cates=$this->gethtmlTree($cates, 0,0,$_SERVER['HTTP_HOST'],1);
        $this->assign('catelevel',1);
        $this->assign('cates',$cates);
        return $this->fetch();
    }
    private function gethtmlTree($data, $f_id,$num=0,$imgurl,$catelevel){
        $html = '';
        $num++;
        foreach($data as $k => $v)
        {
           if($v['f_id'] == $f_id)
           {         //父亲找到儿子
            $html .= '<ol>';
            $html .=    '<li>';
            $html .=        '<div class="tcate">';
            $html .=            '<div><div>';
            if ($num < $catelevel) {
                if($num==1){
                    $html .='<span class="minus">-</span><span class="plus">+</span>';
                }elseif($num==2){
                    $html .='<span style="padding-left:36px;" class="minus">-</span><span class="plus">+</span>';
                }elseif($num==3){
                    $html .='<span style="padding-left:72px;" class="minus">-</span><span class="plus">+</span>';
                }
            }else{
                $html .='<span style="display:none;" class="minus minuslast">-</span><span style="padding-left:36px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="plus">+</span>';
            }
            // $html .=                '</div><img src="'.$v['icon'].'">'.$v['catename'].'</h6></div>';
            $html .=                '</div>';
            // $html .= '<img src="'.$v['icon'].'">';
            $html .= $v['catename'].'</h6></div>';
            
            $html .=            '<div>';
            if ($v['state'] == 2) {
                $html .=                '<a href="javascript:;" onClick="state_on('.$v['id'].');" style="background:#999;" class="storespan btn btn-xs">';
                $html .=                    '<input style="border:none;background:#999;color:#fff;cursor:pointer;" type="button" value="禁用">';
                $html .=                '</a>';
            }
            if ($v['state'] == 1) {
                $html .=                '<a href="javascript:;" onClick="state_on('.$v['id'].');" style="background:#5eb95e;" class="storespan btn btn-xs">';
                $html .=                    '<input style="border:none;background:#5eb95e;color:#fff;cursor:pointer;" type="button" value="启用">';
                $html .=                '</a>';
            }
            $html .=                '<a';
            if ($num < $catelevel) {
                
            }else{
                $html .=' style="display:none"';
            }
            $html .=                    ' href="javascript:;" onclick="cate_add(\'添加分类\',\'/index.php/admin/cates/cate_add.html?f_id='.$v['id'].'\',800,500)" class="storespan btn btn-xs">添加</a>';
            $html .=                '<a href="javascript:;"  onclick="cate_edit(\'编辑分类\',\'/index.php/admin/cates/cate_edit.html?id='.$v['id'].'\','.$v['id'].',800,500)" class="storespan btn btn-xs">编辑</a>';
            $html .=                '<a href="javascript:;" onclick="cate_del(this,'.$v['id'].')" class="storespan btn btn-xs">删除</a>';
            $html .=            '</div>';
            $html .=        '</div>';
            $html .= $this->gethtmlTree($data, $v['id'],$num,$imgurl,$catelevel);
            $html .=    '</li>';
            $html .= '</ol>';
           }
        }
        return $html;
    }
    public function cate_add(){
        if(request()->isGet()){
            $cates=Db::name('cates')->where('state',1)->select();
            if(!empty(input("param.f_id"))){
                $f_id=input("param.f_id");
            }else{
                $f_id=0;
            }
            $this->assign('f_id',$f_id);
            $this->assign('cates',$cates);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param=input('param.');
            $data['f_id']=$param['f_id'];
            // $data['icon']=$param['icon'];
            $data['catename']=$param['catename'];
            $data['intro']=$param['intro'];
            $data['state']=1;
            $res=Db::name('cates')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function cate_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('cates')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            // $data['icon']=$param['icon'];
            $data['catename']=$param['catename'];
            $data['intro']=$param['intro'];
            $data['state']=1;
            $res= Db::name('cates')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_on(){
        $id=input('param.id');
        $state = Db::name('cates')->where('id',$id)->find();
        if($state['state']==1){
            $status = Db::name('cates')->where('id',$id)->update(['state'=>2]);
            if($status){
                return json(['code' => 202,'msg' => '已禁用']);
            }
        }elseif($state['state']==2){
            $status = Db::name('cates')->where('id',$id)->update(['state'=>1]);
            if($status){
                return json(['code' => 201,'msg' => '已启用']);
            }
        }
        return json(['code' => 500,'msg' => '出错']);
    }
    public function cate_del(){
        $param = input('post.');
        $faths=Db::name('cates')->where('state','<',3)->select();
        $this->delall($faths,$param['id']);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    private function delall($faths,$id){
      foreach ($faths as $key => $value) {
        if ($value['id']==$id) {
            Db::name('cates')->where(array('id'=>$value['id']))->update(['state'=>3]);
            $s_id=Db::name('cates')->where('f_id',$value['id'])->where('state','<',3)->select();
            for ($i=0; $i <count($s_id) ; $i++) { 
                $this->delall($faths,$s_id[$i]['id']);
            }
        }
      }
    }
}
